package com.yinhe.music.yhmusic.radio;

import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.RadioSongsAdapter;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceFragment;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.model.RadioListInfo;
import com.yinhe.music.yhmusic.radio.contract.IRadioSongContract;
import com.yinhe.music.yhmusic.radio.presenter.RadioSongPresenter;
import com.yinhe.music.yhmusic.widget.CustomLoadMoreView;
import com.yinhe.music.yhmusic.widget.DividerItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author 何晓华
 * @date 2017/9/26
 */

public class RadioSongFragment extends BaseServiceFragment implements IRadioSongContract.IRadioSongView {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private RadioSongsAdapter mAdapter;
    private int radioId;
    private int mPageNum;
    private int mPageSize = 20;
    private RadioSongPresenter mPresenter;
    private int mPage = 1;

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new RadioSongPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    public void initView() {
        RxBus.get().register(this);
        if (getArguments() != null) {
            radioId = getArguments().getInt("radioid");
        }
        setRecyclerView();

        mPresenter.getRadioSong(radioId, mPageSize, mPage);
    }

    @Override
    public void onDestroyView() {
        RxBus.get().unregister(this);
        super.onDestroyView();
    }

    @Override
    public int getLayoutId() {
        return R.layout.observable_recycler;
    }

    private void setRecyclerView() {
        mAdapter = new RadioSongsAdapter();

        setLoadMore();
        mAdapter.setOnItemClickListener((adapter, view, position) -> playRadioSong(adapter, position));

        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(mAdapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST, 0, true));
    }

    private void playRadioSong(BaseQuickAdapter adapter, int position) {
        List<RadioListInfo.RadioSongs> radioSongsList = adapter.getData();
        List<Music> musicList = new ArrayList<>();
        for (int j = 0; j < radioSongsList.size(); j++) {
            Music music = new Music(radioSongsList.get(j), radioId);
            music.setRadio(true);
            musicList.add(music);
        }
        if (mService != null) {
            mService.playPosition(musicList, position);
        }
    }

    private void setLoadMore() {
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        mAdapter.setOnLoadMoreListener(() -> {
                if (mPage <= mPageNum) {
                    reqSongList();
                } else {
                    mAdapter.loadMoreEnd();
                }
        }, recyclerView);
        mAdapter.setPreLoadNumber(5);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
    }

    private void reqSongList() {
        mPresenter.getRadioSong(radioId, mPageSize,mPage);
    }

    @Override
    public void setRadioSongUI(RadioListInfo radioListInfo) {
        recyclerView.stopScroll();
        List<RadioListInfo.RadioSongs> list = radioListInfo.getRadioSongList();
        if (list == null) {
            return;
        }
        mPageNum = radioListInfo.getPageNum();
        int size = list.size();

        if (mPage == 1) {
            mAdapter.setNewData(list);
        } else {
            if (size > 0) {
                mAdapter.addData(list);
            }
        }

        if (size == mPageSize) {
            if (mPage <= mPageNum) {
                mPage++;
                mAdapter.loadMoreComplete();
            } else {
                mAdapter.loadMoreEnd();
            }
        } else {
            mAdapter.loadMoreEnd();
        }
    }
}
